#include<vector>
#include<ctime>

#include<formic/mpi/interface.h>
#include<formic/random/random.h>
#include<formic/output/output.h>
#include<formic/quicksort.h>
//#include<formic/test_driver/utilities.h>

int main(int argc, char ** argv) {

  formic::mpi::init(argc, argv);

  // open the output file
  formic::open_output_file("output.txt");

  formic::set_seed(std::time(NULL));
  formic::LaggedFibonacci<> lfg;

  const int n = 100000;
  std::vector<int> v(n, 0);
  std::vector<int*> work(n+4, (int*)0);

  for (int i = 0; i < 10; i++) {

    for (std::vector<int>::iterator it = v.begin(); it != v.end(); it++)
      *it = lfg();
    formic::quicksort(&v.at(0), &v.at(0) + v.size(), &work.at(0));
    for (std::vector<int>::iterator it = v.begin()+1; it != v.end(); it++)
      if ( *(it-1) > *it )
        throw formic::Exception("quicksort with supplied work failed to put the vector's elements in ascending order");

    for (std::vector<int>::iterator it = v.begin(); it != v.end(); it++)
      *it = lfg();
    formic::quicksort(&v.at(0), &v.at(0) + v.size());
    for (std::vector<int>::iterator it = v.begin()+1; it != v.end(); it++)
      if ( *(it-1) > *it )
        throw formic::Exception("quicksort without supplied work failed to put the vector's elements in ascending order");

  }

  formic::close_output_file();

  formic::mpi::finalize();

  return EXIT_SUCCESS;

}
